﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication1.SensorTable
{
    public partial class FormCard_Door : Form
    {
        private bool change = false;

        public FormCard_Door()
        {
            InitializeComponent();
        }

        private void FormCard_Door_Load(object sender, EventArgs e)
        {
            try
            {
                Monitor_DB.instance.db.Refresh(RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_card_door);
                this.tb_card_doorBindingSource.DataSource = Monitor_DB.instance.db.tb_card_door;
                
                // 卡号
                var cardQuery =
                    from card in Monitor_DB.instance.db.tb_card
                    select card;
                foreach (var item in cardQuery)
                {
                    dataGridViewComboBoxColumnCard.Items.Add(item.ID);
                }

                // 门号
                var doorQuery =
                    from door in Monitor_DB.instance.db.tb_door
                    select door;
                foreach (var item in doorQuery)
                {
                    dataGridViewComboBoxColumnDoor.Items.Add(item.ID);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tb_card_doorBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            DataTable daorig = new DataTable();
            DataTable dadest = new DataTable();

            int[] check = new int[3] { 0, 0, 0};
            try
            {
                SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlDataAdapter sda2 = new SqlDataAdapter();


                string strSql = "select * from tb_card_door";
                SqlCommand cmd = new SqlCommand(strSql, sqlCon);
                sda.SelectCommand = cmd;
                sda.Fill(daorig);
                tb_card_doorDataGridView.EndEdit();
                Monitor_DB.instance.db.SubmitChanges();
                change = false;

                sda.Fill(dadest);
                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.carddoor, "配置门卡授权", "成功", check);

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception Ex)
            {
                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.carddoor, "配置门卡授权", "失败", check);
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Monitor_DB.instance.Refresh_DB();
            }
        }

        private void card_tb_IDTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // 卡号
                int id = 0;
                id = Convert.ToInt32(this.card_tb_IDTextBox.Text);
                if (id == 0)
                {
                    return;
                }

                var cardQuery =
                    from card in Monitor_DB.instance.db.tb_card
                    where card.ID == id
                    select card;

                if (cardQuery.Count() > 0)
                {
                    this.card_tb_IDTextBox.Text = cardQuery.First().Card_Id;
                }
            }
            catch (Exception Ex)
            {
                //MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            
        }

        private void door_tb_IDTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // 门号
                int id = 0;
                id = Convert.ToInt32(this.door_tb_IDTextBox.Text);
                if (id == 0)
                {
                    return;
                }

                IEnumerable<tb_door> doorQuery =
                    from door in Monitor_DB.instance.db.tb_door
                    where door.ID == id
                    select door;

                foreach (tb_door item in doorQuery)
                {
                    this.door_tb_IDTextBox.Text = item.Name;
                }
            }
            catch (Exception Ex)
            {
                //MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        private void tb_card_doorDataGridView_SelectionChanged(object sender, EventArgs e)
        {
        //    try
        //    {
        //        // 卡号
        //        int id = 0;
        //        id = Convert.ToInt32(this.tb_card_doorDataGridView.SelectedRows[0].Cells["dataGridViewComboBoxColumnCard"].Value.ToString());
        //        if (id == 0)
        //        {
        //            return;
        //        }

        //        var cardQuery =
        //            from card in Monitor_DB.instance.db.tb_card
        //            where card.ID == id
        //            select card;

        //        if (cardQuery.Count() > 0)
        //        {
        //            this.card_tb_IDTextBox.Text = cardQuery.First().Card_Id;
        //        }
        //    }
        //    catch (Exception Ex)
        //    {
        //        MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }

        //    try
        //    {
        //        // 门号
        //        int id = 0;
        //        id = Convert.ToInt32(this.tb_card_doorDataGridView.SelectedRows[0].Cells["dataGridViewComboBoxColumnDoor"].Value.ToString());
        //        if (id == 0)
        //        {
        //            return;
        //        }

        //        IEnumerable<tb_door> doorQuery =
        //            from door in Monitor_DB.instance.db.tb_door
        //            where door.ID == id
        //            select door;

        //        foreach (tb_door item in doorQuery)
        //        {
        //            this.door_tb_IDTextBox.Text = item.Name;
        //        }
        //    }
        //    catch (Exception Ex)
        //    {
        //        MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }
        }

        private void tb_card_doorDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_card_doorBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void FormCard_Door_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (change)
            {
                DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    tb_card_doorBindingNavigatorSaveItem_Click(sender, e);
                }
                else if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
            }
        }

        private void tb_card_doorDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            change = true;
        }


    }
}
